/**
 * File:		Degree.java
 * Project:		Virtual Advisor
 * Module:		Client App
 * Author:		Rotem Ganel
 * Created:		April 30, 2011
 * Updated:		April 30, 2011
 * Version:		1 (in progress)
 */
package clientApp;

import java.io.Serializable;
import java.util.ArrayList;

//Rotem wrote this class 0.o

//use the getSection() methods to get shallow copies of the sections for manipulation.

public class Degree implements Serializable {
	private static final long serialVersionUID = -6018844692200722509L;

	private static int FIRST_SECTION = 'A';

	public String name;
	ArrayList<Section> sections;

	public Degree(String name) {
		this.name = name;
		sections = new ArrayList<Section>();
	}

	public ArrayList<Section> getSections() {
		ArrayList<Section> temp = new ArrayList<Section>();
		for (Section s : sections) {
			temp.add(new Section(s));
		}
		
		return temp;
	}
	
	public boolean addSection(String name) {
		for (Section s : sections) {
			if (s.name == name) {
				return false;
			}
		}

		char identifier = (char)((int)FIRST_SECTION + sections.size());
		sections.add(new Section(identifier, name));
		return true;
	}
	
	public boolean addSection(String name, Section.REQ_TYPE reqType) {
		for (Section s : sections) {
			if (s.name == name) {
				return false;
			}
		}

		char identifier = (char)((int)FIRST_SECTION + sections.size());
		sections.add(new Section(identifier, name, reqType));
		return true;
	}
	
	public boolean addSection(String name, Section.REQ_TYPE reqType, int unitsNeeded) {
		for (Section s : sections) {
			if (s.name == name) {
				return false;
			}
		}

		char identifier = (char)((int)FIRST_SECTION + sections.size());
		sections.add(new Section(identifier, name, reqType, unitsNeeded));
		return true;
	}

	public boolean removeSection(char identifier) {
		int index = (int)identifier - FIRST_SECTION;
		int numSections = sections.size();
		
		if ((index < 0) || (index >= sections.size())) {
			return false;
		}
		
		sections.remove(index);
		
		if (sections.size() < numSections) {
			for (int i = index; i < sections.size(); i++) {
				sections.get(i).identifier = (char)(i + FIRST_SECTION);
			}

			return true;
		} else {
			return false;
		}
	}
	
	public boolean removeSection(String name) {
		return removeSection(getSectionIdentifier(name));
	}
	
	//shallow copy
	public Section getSection(char identifier) {
		int index = (int)identifier - FIRST_SECTION;

		if ((index < 0) || (index >= sections.size())) {
			return null;
		} else {
			return (sections.get(index));
		}
	}
	
	//shallow copy
	public Section getSection(String name) {
		return getSection(getSectionIdentifier(name));
	}
	
	public Section.REQ_TYPE getReqType(char identifier) {		
		int index = (int)identifier - FIRST_SECTION;
		
		if ((index < 0) || (index >= sections.size())) {
			return Section.REQ_TYPE.INVALID;
		} else {
			return sections.get(index).getReqType();
		}
	}
	
	public Section.REQ_TYPE getReqType(String name) {
		return getReqType(getSectionIdentifier(name));
	}
	
	public int getUnitsNeeded(char identifier) {
		int index = (int)identifier - FIRST_SECTION;
		
		if ((index < 0) || (index >= sections.size())) {
			return -1;
		} else {
			return sections.get(index).getUnitsNeeded();
		}
	}
	
	public int getUnitsNeeded(String name) {
		return getUnitsNeeded(getSectionIdentifier(name));
	}
	
	public boolean setReqType(char identifier, Section.REQ_TYPE reqType) {
		int index = (int)identifier - FIRST_SECTION;
		
		if ((index < 0) || (index >= sections.size())) {
			return false;
		} else {
			return sections.get(index).setReqType(reqType);
		}
	}
	
	public boolean setReqType(String name, Section.REQ_TYPE reqType) {
		return setReqType(getSectionIdentifier(name), reqType);
	}
	
	public boolean setUnitsNeeded(char identifier, int unitsNeeded) {
		int index = (int)identifier - FIRST_SECTION;
		
		if ((index < 0) || (index >= sections.size())) {
			return false;
		} else {
			return sections.get(index).setUnitsNeeded(unitsNeeded);
		}
	}
	
	public boolean setUnitsNeeded(String name, int unitsNeeded) {
		return setUnitsNeeded(getSectionIdentifier(name), unitsNeeded);
	}
	
	public String[] listSections() {
		String[] sections = new String[this.sections.size()];
		
		for (int i = 0; i < sections.length; i++) {
			sections[i] = this.sections.get(i).name;
		}
		
		return sections;
	}
	
	public String[] getAllCoursesInSection(char identifier) {		
		int index = (int)identifier - FIRST_SECTION;
		if ((index < 0) || (index >= sections.size())) {
			return null;
		} else {
			return sections.get(index).getAllCourses();
		}
	}
	
	public String[] getAllCoursesInSection(String name) {
		return getAllCoursesInSection(getSectionIdentifier(name));
	}
		
	//Returns null if not found
	public String getSectionName(char identifier) {
		int index = (int)identifier - FIRST_SECTION;
		
		if ((index < 0) || (index >= sections.size())) {
			return null;
		} else {
			return sections.get(index).name;
		}
	}
	
	//Returns 1 if not found
	public char getSectionIdentifier(String name) {
		for (Section s : sections) {
			if (s.name.equals(name)) {
				return s.identifier;
			}
		}
		
		return '1';
	}
}
